//
//  ST_PrivateInviteChatTimesLog.swift
//  btvideo
//
//  Created by Jason on 2021/12/14.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import RealmSwift

class ST_PrivateInviteChatTimesLog: Object {
    
    @objc dynamic var myId = ""
    
    @objc dynamic var target_accid = ""
    /// 1 语音 2视频
    @objc dynamic var chat_type = 1
    
    @objc dynamic var date = Date()
    
}

extension ST_PrivateInviteChatTimesLog{
    
    /// 能否邀请
    class func canInvite(type:Int,targetId:String)->Bool{
        
        guard let accid = ST_BTUserInfoManager.shared.current_account_info?.accid else {
            return false
        }
        
        let realm = try! Realm()
        
        
        if  realm.objects(ST_PrivateInviteChatTimesLog.self).filter("myId = %@ AND  target_accid = %@",accid,targetId).first(where: {  $0.chat_type == type && $0.date.isInToday }) != nil{
            return false
        }
        
        return true
    }
    
    /// 插入数据
    class func insert(type:Int,targetId:String){
        
        guard let accid = ST_BTUserInfoManager.shared.current_account_info?.accid else {
            return
        }
        
        let m = ST_PrivateInviteChatTimesLog()
        m.myId = accid
        m.target_accid = targetId
        m.chat_type = type
        
        let realm = try! Realm()
        try! realm.write{
            realm.add(m)
        }
        
    }
    
    
}

